import React, {Component} from 'react';
import './index.less';
import {base} from 'nc-lightapp-front';

const {NCModal, NCButton} = base;
const {Header: NCModalHeader, Body: NCModalBody, Footer: NCModalFooter} = NCModal;

class AlertModal extends Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            message: 'confirm'
        };
        this.show = this.show.bind(this);
        this.handleNext = this.handleNext.bind(this);
        this.beSure = this.beSure.bind(this);
        this.beDeny = this.beDeny.bind(this);
    }

    show(confirmMsg, warningMsg, callback) {
        if ((!confirmMsg && !warningMsg) || (!confirmMsg.length && !warningMsg.length)) {
            callback && callback();
            return;
        }
        this.confirmMsg = confirmMsg;
        this.warningMsg = warningMsg;
        this.message = '';
        this.callback = callback;
        this.setState({
            type: 'confirm'
        }, this.handleNext)

    }

    handleNext() {
        const {type} = this.state;
        if (type === "confirm") {
            if (!this.confirmMsg || !this.confirmMsg[0]) {
                if (!this.warningMsg || !this.warningMsg[0]) {
                    this.setState({
                        visible: false
                    });
                    this.callback && this.callback();
                } else {
                    this.message = this.warningMsg.splice(0, 1);
                    this.setState({
                        type: 'warning',
                        visible: true
                    })
                }
            } else {
                this.message = this.confirmMsg.splice(0, 1);
                this.setState({
                    visible: true
                })
            }
        } else {
            if (!this.warningMsg || !this.warningMsg[0]) {
                this.setState({
                    visible: false
                });
                this.callback && this.callback();
            } else {
                this.message = this.warningMsg.splice(0, 1);
                this.setState({
                    visible: true
                })
            }
        }
    }

    beSure() {
        this.handleNext();
    }

    beDeny() {
        this.setState({
            visible: false
        })
    }

    render() {
        const {visible, type} = this.state;
        return (
            <NCModal
                show={visible}
                size="sm"
                backdrop={'static'}
            >
                <NCModalHeader>
                    {type === 'confirm' ? this.props.json['hi6007-000174'] : this.props.json['hi6007-000102']}{/* 国际化处理： 确认,警告,确认*/}
                </NCModalHeader>
                <NCModalBody>
                    <div
                        dangerouslySetInnerHTML={{
                            __html: this.message
                        }}
                    />
                </NCModalBody>
                <NCModalFooter>
                    <NCButton colors="primary" onClick={this.beSure}>
                        {type === 'confirm' ? this.props.json['hi6007-000175'] : this.props.json['hi6007-000174']}{/* 国际化处理： 是,确认,是,确认*/}
                    </NCButton>
                    {type === 'confirm' ? <NCButton shape="border" onClick={this.beDeny}>
                        {this.props.json['hi6007-000176']}{/* 国际化处理： 否*/}
                    </NCButton> : null}
                </NCModalFooter>
            </NCModal>
        );
    }
}

export default AlertModal;
